﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProxyWrapper;
public partial class UserCreated : SecPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            ProxyWrapper.UserProxy.UserDetails _objUser = ((ProxyWrapper.UserProxy.UserDetails)Session["UserObject"]);
            int _dLimit = 0;
            int _userid = 0;
            List<string> lstServices = null;
            ProxyWrapper.UserServiceWrapper _user = new UserServiceWrapper();
            string userId = Request["userId"].ToString();
            string userName = Request["username"].ToString();
            string password = Request["password"].ToString();
            string deviceLimit = Request["deviceLimit"].ToString();
            string serviceList = Request["selectedServices[]"]!=null ?Request["selectedServices[]"].ToString():string.Empty;
            if (!string.IsNullOrWhiteSpace(serviceList))
            {
                lstServices = new List<string>();
                lstServices = serviceList.Split(',').ToList<string>();
            }

            Int32.TryParse(deviceLimit, out _dLimit);
            Int32.TryParse(userId, out _userid);
            int _clientId = 0;
            if (_objUser.RoleId == 2)
                _clientId = _objUser.UserId;
            else
                _clientId = _objUser.ClientId;

            string msg = _user.AddAppUser(_userid, "", userName, password, "", _dLimit,_clientId, lstServices, _objUser.HashCode);
            string _messgae = "";
            if (msg == "0" && _userid > 0)
            {
                _messgae = "User information saved.";
            }
            else if (msg == "0" && _userid==0)
            {
                _messgae = "New user created.";
            }

            if (_messgae.Trim().Length > 0)
            {
                Response.Redirect("Users.aspx?msg=" + _messgae,false);
            }
            else
            {
                Response.Redirect("CreateUser.aspx?msg=" + msg,false);
            }
        }
        catch (Exception ex)
        {
            Response.Write("Error while creating user " + ex.Message);
        }
    }
}